home *** CD-ROM | disk | FTP | other *** search
- //--------------------------------------------------------------------------
- //
- // OBJCALLB.CPP
- //
- // Object Thunk functions
- //
- //--------------------------------------------------------------------------
-
- #include "objcallb.h"
- #include <mem.h>
-
-
- const int CNTJumpInstance=64;
-
- //
- // AllocCSToDSAlias was not declared in windows.h
- //
- extern "C" WORD FAR PASCAL AllocCSToDSAlias(WORD);
-
- //--------------------------------------------------------------------------
- //
- // Define data structures.
- //
- //--------------------------------------------------------------------------
-
- //
- // This is the Jump Block, which is basically a thunk with the
- // following assembler code:
- //
- // mov bx, SEGT
- // mov es, bx
- // mov bx, OFFT
- // jmp FAR FarJump
- //
-
-
- #pragma option -a- // force byte alignment
-
- typedef struct JumpThunk {
- BYTE ins1; // 0xBB
- WORD wThisSeg;
- BYTE ins2[3]; // 0x8E 0xC3 0xBB
- WORD wThisOff;
- BYTE ins3; // 0xEA
- union Ptr {
- struct JumpThunk FAR *Next;
- FARPROC fpJump;
- } ptr;
- BYTE reserved[3];// make an even 16 bytes
- } FAR *PJumpThunk;
-
- #pragma option -a. // default alignment
-
-
- static char HeaderSignature[] = "ThunkMaster";
-
- typedef struct JumpBlock {
- WORD NextBlock;
- BYTE HeaderInfo[sizeof(HeaderSignature)];
- JumpThunk Instances[CNTJumpInstance];
- } FAR *PJumpBlock;
-
- //--------------------------------------------------------------------------
- //
- // Define global variables
- //
- //--------------------------------------------------------------------------
-
- WORD InstBlockList = 0;
- PJumpThunk InstFreeList = NULL;
-
-
- //--------------------------------------------------------------------------
- //
- // MakeObjectThunk - This allocates an instance thunk
- //
- //--------------------------------------------------------------------------
- FARPROC WINAPI MakeObjectThunk(FARPROC CallBack, void far *fpObject)
- {
- PJumpBlock Block;
- PJumpThunk Instance;
- FARPROC ObjInstance;
-
- //
- // If there are no free object thunk blocks available,
- // allocate a new one.
- //
- if (InstFreeList==NULL) {
- Block = (PJumpBlock) GlobalLock(GlobalAlloc(
- GMEM_FIXED | GMEM_DDESHARE, sizeof(JumpBlock)));
- Block->NextBlock = InstBlockList;
- _fmemcpy(Block->HeaderInfo, HeaderSignature,
- sizeof(HeaderSignature));
-
- Instance = Block->Instances;
-
- for(int i=0; i<CNTJumpInstance; ++i, ++Instance) {
- Instance->ins1 = 0xBB;
- Instance->ins2[0] = 0x8E;
- Instance->ins2[1] = 0xC3;
- Instance->ins2[2] = 0xBB;
- Instance->ins3 = 0xEA;
- Instance->ptr.Next = InstFreeList;
- InstFreeList = Instance;
- }
-
- InstBlockList = SELECTOROF(Block);
-
- PrestoChangoSelector(SELECTOROF(Block), SELECTOROF(Block));
- }
-
- ObjInstance = (FARPROC)InstFreeList;
-
- Instance = (PJumpThunk) MAKELP(
- AllocCSToDSAlias(SELECTOROF(InstFreeList)),
- OFFSETOF(InstFreeList));
-
- InstFreeList = Instance->ptr.Next;
- Instance->ptr.fpJump = CallBack;
- Instance->wThisSeg = SELECTOROF(fpObject);
- Instance->wThisOff = OFFSETOF(fpObject);
- FreeSelector(SELECTOROF(Instance));
-
- return (ObjInstance);
- }
-
-
- //--------------------------------------------------------------------------
- //
- // FreeObjectThunk
- //
- // Warning! This does NOT de-allocate Thunk blocks if they
- // are fully un-used. It probably should, but when the module
- // is unloaded, they seem to be deleted anyway.
- //
- //--------------------------------------------------------------------------
- void WINAPI FreeObjectThunk(FARPROC P)
- {
- PJumpThunk Instance;
-
- Instance = (PJumpThunk) MAKELP(
- AllocCSToDSAlias(SELECTOROF(P)),
- OFFSETOF(P) );
-
- Instance->ptr.Next = InstFreeList;
- FreeSelector(SELECTOROF(Instance));
- InstFreeList = (PJumpThunk)P;
- }
-